package main

import (
	"encoding/json"
	"fmt"
	db2 "golang-learning/gorm/db"
	"golang-learning/gorm/model"
	"log"
)

/**
参考：https://gorm.io/zh_CN/docs/connecting_to_the_database.html
*/
func main() {
	/*获得连接*/
	db, err := db2.GetMysqlDbConn()

	if err != nil {
		fmt.Println(err)
		return
	}

	var baseChnlList []model.BaseChnl
	db.Debug().Find(&baseChnlList)
	log.Println(baseChnlList)
	log.Println(baseChnlList[0])

	rs, err := json.Marshal(baseChnlList)
	if err != nil {
		log.Fatalln(err)
	}

	log.Println("baseChnlList:", string(rs))

	var baseChnl model.BaseChnl
	db.Debug().First(&baseChnl, 1)
	log.Println(baseChnl)

	var baseChnl2 model.BaseChnl
	db.Debug().Where("id = ?", 2).First(&baseChnl2)
	log.Println(baseChnl2)

	var baseChnl3 model.BaseChnl
	db.Debug().Where(&model.BaseChnl{Id: 3}).First(&baseChnl3)
	log.Println(baseChnl3)

	var baseChnl4 model.BaseChnl
	db.Debug().Where(map[string]interface{}{"id": "4"}).Find(&baseChnl4)

	var baseChnlList2 []model.BaseChnl
	db.Debug().Where([]int64{1, 2}).Find(&baseChnlList2)
	log.Println(baseChnlList2)

}
